package com.innovation.chengzhangriji.ui.widget;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.innovation.chengzhangriji.R;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MessageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public interface OnClickMessageListener {

        void onClickMessage(MessageViewHolder viewHolder);

    }

    private OnClickMessageListener mOnClickMessageListener;

    private CuteTextView mTextName;

    private CuteTextView mTextRole;

    private CuteTextView mTextConfirm;

    private CuteTextView mTextTime;

    private CuteTextView mTextContent;

    private CuteBabyAvatarView mImage;

    private CuteTextView mTextMsg;

    private ImageView mImageNew;

    private ImageView mImageHandle;

    private DisplayImageOptions options;

    public MessageViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        mTextName = (CuteTextView) itemView.findViewById(R.id.text_name);
        mTextRole = (CuteTextView) itemView.findViewById(R.id.text_role);
        mTextConfirm = (CuteTextView) itemView.findViewById(R.id.text_confirm);
        mTextTime = (CuteTextView) itemView.findViewById(R.id.text_time);
        mTextContent = (CuteTextView) itemView.findViewById(R.id.text_message);
        mImage = (CuteBabyAvatarView) itemView.findViewById(R.id.image_head);
        mTextMsg = (CuteTextView) itemView.findViewById(R.id.text_msg);
        mImageNew = (ImageView) itemView.findViewById(R.id.image_new);
        mImageHandle = (ImageView) itemView.findViewById(R.id.image_handle);
        setOptions();
    }

    @Override
    public void onClick(View v) {
        if (mOnClickMessageListener != null) {
            mOnClickMessageListener.onClickMessage(this);
        }
    }

    public void setOnClickMessageListener(OnClickMessageListener onClickMessageListener) {
        mOnClickMessageListener = onClickMessageListener;
    }

    public void setName(String text) {
        mTextName.setText(text);
    }

    public void setRole(String text) {
        mTextRole.setText(text);
    }

    public void setImageUrl(String imageUrl, int sex) {
        // ImageLoader.getInstance().displayImage(imageUrl, mImage, options);
        mImage.setAvatar(imageUrl);
        mImage.setSex(sex);
    }

    public void setTime(String time) {
        mTextTime.setText(time);
    }

    public void setContent(String text) {
        mTextContent.setText(text);
    }

    public void setMsg(String text) {
        mTextMsg.setText(text);
    }

    public void setConfirm(String text) {
        if (TextUtils.isEmpty(text)) {
            mTextConfirm.setVisibility(View.GONE);
        } else {
            mTextConfirm.setVisibility(View.VISIBLE);
            mTextConfirm.setText(text);
        }
    }

    public void visibleNew() {
        mImageNew.setVisibility(View.VISIBLE);
    }

    public void invisibleNew() {
        mImageNew.setVisibility(View.INVISIBLE);
    }

    public void setHandle(Drawable drawable) {
        if (drawable == null) {
            mImageHandle.setVisibility(View.GONE);
        } else {
            mImageHandle.setVisibility(View.VISIBLE);
            mImageHandle.setImageDrawable(drawable);
        }
    }

    private void setOptions() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_head_unknown)    //在ImageView加载过程中显示图片
                .showImageForEmptyUri(R.drawable.ic_head_unknown)  //image连接地址为空时
                .showImageOnFail(R.drawable.ic_head_unknown)  //image加载失败
                .cacheOnDisk(true)   //加载图片时会在磁盘中加载缓存
                .build();
    }
}
